﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace CTEA_INDIA
{
    public partial class ViewAllTechnical : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["CTEA"].ConnectionString;

         
            if (!IsPostBack)
            {
                if (Session["UserID"] == null)
                {
                    Response.Redirect("Default.aspx");
                }

                PopulateViewAllTechnical();
            }
        }

        public void PopulateViewAllTechnical()
        {
            DataSet ds = BAL.Customers.ViewAllTechnical();
            //if (ds.Tables.Count > 0)
            //{
            //    if (ds.Tables[0].Rows.Count > 0)
            //    {
                    gdTechnicalProfile.DataSource = ds;
                    gdTechnicalProfile.DataBind();

                //}

                //else
                //{
                    //spanerrormsg.Style.Add("display", "block");
                    //spanerrormsg.InnerHtml = "Data not found";
            //    }
            //}
            
        }

        protected void gdTechnicalProfile_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lEdit = (LinkButton)e.Row.FindControl("lnkEdit");
                Button bUpdate = (Button)e.Row.FindControl("btnUpdate");
                LinkButton lDelete = (LinkButton)e.Row.FindControl("lnkDelete");

                lEdit.CommandArgument = e.Row.RowIndex.ToString();
                bUpdate.CommandArgument = e.Row.RowIndex.ToString();
                lDelete.CommandArgument = e.Row.RowIndex.ToString();

            }
        }


        protected void lnkEdit_Click(object sender, EventArgs e)
        {
            LinkButton lnEdit = (LinkButton)sender;
            int rowIndex = Convert.ToInt32(lnEdit.CommandArgument);

            AjaxControlToolkit.ModalPopupExtender m = (AjaxControlToolkit.ModalPopupExtender)lnEdit.Parent.Parent.FindControl("lnkEdit_ModalPopupExtender");
            m.TargetControlID = "lnkEdit";
            m.Show();

            int ID = Convert.ToInt32(gdTechnicalProfile.DataKeys[rowIndex]["UserId"]);
            Panel gvPanel = (Panel)gdTechnicalProfile.Rows[rowIndex].Cells[0].FindControl("pnlLnkEdit");

            string[] details = BAL.Customers.GetTechnicalById(ID);

            TextBox tName = (TextBox)gvPanel.FindControl("txtName");
             tName.Text=details[2];

            TextBox tAdd = (TextBox)gvPanel.FindControl("txtAdd");
            tAdd.Text=details[4];

            TextBox tMob = (TextBox)gvPanel.FindControl("txtMob");
             tMob.Text=details[6];

            TextBox tDob = (TextBox)gvPanel.FindControl("txtDob");
           tDob.Text=details[7];

            TextBox tEmail = (TextBox)gvPanel.FindControl("txtEmail");
            tEmail.Text=details[3];

            TextBox tLoc = (TextBox)gvPanel.FindControl("txtLocation");
             tLoc.Text=details[5];

            //TextBox tTechType = (TextBox)gvPanel.FindControl("txtTechType");
            //tTechType.Text=details[1];

        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Button bUpdate = (Button)sender;
            int rowIndex =Convert.ToInt32( bUpdate.CommandArgument);

            int UserId = Convert.ToInt32(gdTechnicalProfile.DataKeys[rowIndex]["UserId"]);

            Panel gvPanel = (Panel)gdTechnicalProfile.Rows[rowIndex].Cells[0].FindControl("pnlLnkEdit");

            TextBox tnm = (TextBox)gvPanel.FindControl("txtName");
            string name = tnm.Text;

            TextBox tadd = (TextBox)gvPanel.FindControl("txtAdd");
            string add = tadd.Text;

            TextBox tmob = (TextBox)gvPanel.FindControl("txtMob");
            string mob = tmob.Text;

            TextBox tdate = (TextBox)gvPanel.FindControl("txtDob");
            DateTime  dob =Convert.ToDateTime( tdate.Text);

            TextBox temail = (TextBox)gvPanel.FindControl("txtEmail");
            string email = temail.Text;

            TextBox tloc = (TextBox)gvPanel.FindControl("txtLocation");
            string loc = tloc.Text;
     
            BAL.Customers.UpdateProfAdminTech(name, add, loc, dob, mob, UserId );

            PopulateViewAllTechnical();

        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            LinkButton lnDelete = (LinkButton)sender;
            int rowIndex = Convert.ToInt32(lnDelete.CommandArgument);

            int userId =Convert.ToInt32( gdTechnicalProfile.DataKeys[rowIndex]["UserId"]);

            BAL.Customers.IsDeleteTechnical(userId);

            PopulateViewAllTechnical();

        }

        protected void gdTechnicalProfile_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdTechnicalProfile.PageIndex = e.NewPageIndex;
            PopulateViewAllTechnical();
        }

    }
}
